<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="100"
    >
      <el-form-item
        label="供应日期"
        prop="date"
      >
        <el-date-picker
          style="width: 300px;"
          v-model="queryParams.date"
          type="date"
          @change="handleQuery"
          value-format="yyyy-MM-dd"
          placeholder="选择供应日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="所属公司"
        prop="companyId"
      >
        <el-select
          v-model="queryParams.companyId"
          placeholder="请选择所属公司"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="item in companys"
            :key="item.id"
            :label="item.text"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>
    <!--    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100">-->
    <!--      <el-form-item label="就餐类型id" prop="diningType">-->
    <!--        <el-select v-model="queryParams.diningType" placeholder="请选择就餐类型id" clearable size="small">-->
    <!--          <el-option label="请选择字典生成" value="" />-->
    <!--        </el-select>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="菜品id" prop="foodId">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.foodId"-->
    <!--          placeholder="请输入菜品id"-->
    <!--          clearable-->
    <!--          size="small"-->
    <!--          @keyup.enter.native="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
    <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:foodMenu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:foodMenu:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:foodMenu:delete']"
          >删除</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:foodMenu:export']"
          >导出</el-button>
        </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="menuList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <!-- <el-table-column label="序号" align="center" prop="menuId" /> -->
      <el-table-column
        fixed
        label="序号"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column
        label="供应日期"
        align="center"
        prop="date"
      />
      <el-table-column
        label="早餐"
        align="center"
        prop="breakfastCount"
      >
        <template slot-scope="scope">
          <div
            class="listFood"
            v-for="item,index in scope.row.breakfast"
            :key="index"
          >
<!--            <img :src="item.foodPhoto" />-->
            <image-preview :src="item.foodPhoto" :width="50" :height="50"/>
            <div class="foodInfo">
              <div>{{item.foodName}}</div>
              <div>￥：{{item.foodPrice}} x1</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="午餐"
        align="center"
        prop="lunchCount"
      >
        <template slot-scope="scope">
          <div
            class="listFood"
            v-for="item,index in scope.row.lunch"
            :key="index"
          >
<!--            <img :src="item.foodPhoto" />-->
            <image-preview :src="item.foodPhoto" :width="50" :height="50"/>
            <div class="foodInfo">
              <div>{{item.foodName}}</div>
              <div>￥：{{item.foodPrice}} x1</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="晚餐"
        align="center"
        prop="dinnerCount"
      >
        <template slot-scope="scope">
          <div
            class="listFood"
            v-for="item,index in scope.row.dinner"
            :key="index"
          >
<!--            <img :src="item.foodPhoto" />-->
            <image-preview :src="item.foodPhoto" :width="50" :height="50"/>
            <div class="foodInfo">
              <div>{{item.foodName}}</div>
              <div>￥：{{item.foodPrice}} x1</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="接待餐"
        align="center"
        prop="receptionCount"
      >
        <template slot-scope="scope">
          <div
            class="listFood"
            v-for="item,index in scope.row.reception"
            :key="index"
          >
<!--            <img :src="item.foodPhoto" />-->
            <image-preview :src="item.foodPhoto" :width="50" :height="50"/>
            <div class="foodInfo">
              <div>{{item.foodName}}</div>
              <div>￥：{{item.foodPrice}} x1</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="其它"
        align="center"
        prop="receptionCount"
      >
        <template slot-scope="scope">
          <div
            class="listFood"
            v-for="item,index in scope.row.other"
            :key="index"
          >
<!--            <img :src="item.foodPhoto" />-->
            <image-preview :src="item.foodPhoto" :width="50" :height="50"/>
            <div class="foodInfo">
              <div>{{item.foodName}}</div>
              <div>￥：{{item.foodPrice}} x1</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="所属公司"
        align="center"
        prop="companyName"
      />
      <!-- <el-table-column label="接待餐" align="center" prop="receptionCount" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:foodMenu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:foodMenu:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="所属公司"
          prop="companyId"
        >
          <el-select
            v-model="form.companyId"
            placeholder="请选择所属公司"
            clearable
            size="small"
            style="width: 100%;"
            @change="companyChange"
            :disabled="notModifyCompany"
          >
            <el-option
              v-for="item in companys"
              :key="item.id"
              :label="item.text"
              :value="Number(item.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="供应日期"
          prop="date"
        >
          <el-date-picker
            style="width: 300px;"
            v-model="dateTime"
            type="date"
            @change="dateChange"
            value-format="yyyy-MM-dd"
            placeholder="选择供应日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item
          v-for="(time,index) in timeArr"
          :key="index"
          :label="time.text"
        >
          <el-select
            v-model="breakfastFoods[index]"
            multiple
            placeholder="请选择"
            style="width: 300px;"
          >
            <el-option
              v-for="item in foods"
              :key="item.id"
              :label="item.text"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="就餐类型id">
            <el-select v-model="form.diningType" placeholder="请选择就餐类型id">
              <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
          <el-form-item label="菜品id" prop="foodId">
            <el-input v-model="form.foodId" placeholder="请输入菜品id" />
          </el-form-item> -->
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import {
  diningtypeComboBox,
  comboBox,
  listMenu,
  getMenu,
  delMenu,
  addMenu,
  updateMenu,
  exportMenu,
} from "@/api/foodMenu/menu";
import { getCompanys } from "@/api/system/dept";

export default {
  name: "Menu",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      menuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuId: undefined,
        diningType: undefined,
        foodId: undefined,
        date: undefined,
      },
      // 表单参数
      form: {},
      // 公司信息
      companys: [],
      // 公司能否修改
      notModifyCompany: false,
      menuId: 0,
      dateTime: "",
      breakfastFoods: [],
      foods: [],
      timeArr: [],
      // 表单校验
      rules: {
        menuId: [
          { required: true, message: "菜单id不能为空", trigger: "blur" },
        ],
        diningType: [
          { required: true, message: "就餐类型id不能为空", trigger: "blur" },
        ],
        foodId: [
          { required: true, message: "菜品id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCompanyList();
  },
  methods: {
    companyChange(e) {
      console.log(e);
      diningtypeComboBox(e).then((data) => {
        console.log(data);
        this.timeArr = data.data;
        comboBox(e).then((res) => {
          console.log(res);
          this.foods = res.data;
        });
      });
    },
    /** 查询【请填写功能名称】列表 */
    dateChange(e) {
      console.log(e);
      if (!this.testDate(new Date(e).getTime())) {
        this.msgError("请选择今天及以后的时间");
      }
    },
    testDate(time) {
      const date = new Date();
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      let nowTime = [year, month, day].map(this.formatNumber).join("-");
      if (new Date(nowTime).getTime() > time) {
        return false;
      } else {
        return true;
      }
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then((response) => {
        this.menuList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.dateTime = "";
      this.breakfastFoods = [];
      this.timeArr = [];
      this.menuId = 0;
      this.date = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.menuId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.notModifyCompany = false;
      this.open = true;
      this.form.companyId = "";
      this.title = "添加菜单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.menuId || this.ids[0];
      console.log(row);
      comboBox(row.companyId).then((e) => {
        console.log(e);
        this.foods = e.data;
      });
      getMenu(id).then((response) => {
        this.dateTime = response.data.date;
        this.menuId = response.data.menuId;
        for (let i in response.data.meals) {
          this.timeArr.push({
            id: response.data.meals[i].diningType,
            text: response.data.meals[i].diningTypeName,
          });
          let arr = [];
          for (let y in response.data.meals[i].foods) {
            arr.push(String(response.data.meals[i].foods[y].id));
          }
          this.breakfastFoods[i] = arr;
        }
        this.form = response.data;
        this.notModifyCompany = true;
        this.open = true;
        this.title = "修改菜单";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      let meal = [];
      for (let i in this.breakfastFoods) {
        let food = [];
        for (let y in this.breakfastFoods[i]) {
          food.push({ id: this.breakfastFoods[i][y] });
        }
        meal.push({
          diningType: this.timeArr[i].id,
          foods: food,
        });
      }
      if (this.menuId > 0) {
        console.log(this.menuId);
        updateMenu({
          companyId: this.form.companyId,
          menuId: this.menuId,
          date: this.dateTime,
          meals: meal,
        }).then((e) => {
          console.log(e);
          this.msgSuccess("修改成功");
          this.getList();
          this.open = false;
        });
      } else {
        addMenu({
          companyId: this.form.companyId,
          date: this.dateTime,
          meals: meal,
        }).then((e) => {
          console.log(e);
          this.msgSuccess("新增成功");
          this.getList();
          this.open = false;
        });
      }
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateMenu(this.form).then(response => {
      //         if (response.code == 0) {
      //           this.msgSuccess("修改成功");
      //           this.open = false;
      //           this.getList();
      //         }
      //       });
      //     } else {
      //       addMenu(this.form).then(response => {
      //         if (response.code == 0) {
      //           this.msgSuccess("新增成功");
      //           this.open = false;
      //           this.getList();
      //         }
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const ids = row.id || row.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delMenu(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportMenu(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
    /** 获取公司信息 */
    getCompanyList() {
      getCompanys().then((response) => {
        this.companys = response.data;
      });
    },
  },
};
</script>
  <style>
.listFood {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  padding-bottom: 10px;
  margin-left: 35px;
}
.foodInfo {
  flex-shrink: 0;
  text-align: left;
}
.listFood img {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}
</style>
